package com.lihua.newideas.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lihua.newideas.mapper.ArticleMapper;
import com.lihua.newideas.mapper.CommentMapper;
import com.lihua.newideas.pojo.Article;
import com.lihua.newideas.pojo.Comment;
import com.lihua.newideas.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private CommentMapper commentMapper;


    @Override
    public String addArticle(Article article) {
        articleMapper.addArticle(article);
        return "添加文章成功";
    }

    @Override
    public String updateArticleById(Article article) {
        articleMapper.updateArticleById(article);
        return "修改成功";
    }

    /*
    * 分页查询全部文章
    * */
    @Override
    public PageInfo<Article> queryArticleAll(int page, int limit) {
        String orderBy = "id desc"; //排序
        PageHelper.startPage(page, limit,orderBy);
        List<Article> articles = articleMapper.queryArticleAll();
//        System.out.println(articles.toArray());
        PageInfo<Article> articlesPageInfo = new PageInfo<>(articles);
        return articlesPageInfo;
    }

    /*根据id查询*/
    @Override
    public Article queryArticleById(Long id) {
        Article article = articleMapper.queryArticleById(id);
        if (article.getArticleComment()==0){  //开启了评论
            List<Comment> comments = commentMapper.queryCommentByArticleId(article.getId());
            System.out.println(comments.toArray());
            article.setComments(comments);
            return article;
        }else {
            return article;
        }
        //判断文章是否开启评论


    }

    /*
     * 分页查询指定状态的文章
     * */
    @Override
    public PageInfo<Article> queryArticleByArticleStatus(int articleStatus,int page, int limit) {
        String orderBy = "id desc"; //排序
        PageHelper.startPage(page, limit,orderBy);
        List<Article> articles = articleMapper.queryArticleByArticleStatus(articleStatus);
//        System.out.println(articles.toArray());
        PageInfo<Article> articlesPageInfo = new PageInfo<>(articles);
        return articlesPageInfo;
    }

    @Override
    public String deleteArticleById(long id) {
        articleMapper.deleteArticleById(id);
        return "删除成功";
    }

    @Override
    public List<Article> queryArticleByArticleTitle(String articleTitle) {

        return articleMapper.queryArticleByArticleTitle(articleTitle);
    }

    @Override
    public int queryArticleNumByArticleStatus(long articleStatus) {
        return articleMapper.queryArticleNumByArticleStatus(articleStatus);
    }
}
